package cn.community.system.web.controller;

import cn.community.system.domain.Association;
import cn.community.system.domain.Device;
import cn.community.system.query.QueryObject;
import cn.community.system.service.IDeviceService;
import cn.community.system.util.JsonResult;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/device")
public class DeviceController {
    @Autowired
    private IDeviceService deviceService;
    
    @RequestMapping("/list")
    @RequiresPermissions(value = {"device/list","超管-设备列表"},logical = Logical.OR)
    public String list(Model model, @ModelAttribute("qo")QueryObject qo){
        PageInfo pageInfo = deviceService.query(qo);
        model.addAttribute("page", pageInfo);
        return "device/admin/listDevice";
    }

    @RequestMapping("/listAsso")
    @RequiresPermissions(value = {"device/listAsso","社团-设备列表"},logical = Logical.OR)
    public String listAsso(Model model, @ModelAttribute("qo")QueryObject qo){
        PageInfo pageInfo = deviceService.query(qo);
        model.addAttribute("page", pageInfo);
        return "device/association/listDevice";
    }

    @RequestMapping("/listAll")
    @ResponseBody
    public Object listAll(){
        List<Device> devices = deviceService.listAll();
        return JsonResult.success(devices);
    }

    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    @RequiresPermissions(value = {"device/saveOrUpdate","超管-设备新增/编辑功能"},logical = Logical.OR)
    public Object saveOrUpdate(Device device){
        if(device.getId() == null){ // 代表新增
            deviceService.save(device);
        }else { // 代表修改
            deviceService.update(device);
        }
        return JsonResult.success();
    }


    @RequestMapping("/delete")
    @ResponseBody
    @RequiresPermissions(value = {"device/delete","超管-设备删除功能"},logical = Logical.OR)
    public JsonResult delete(Long id){
        deviceService.delete(id);
        return JsonResult.success();
    }
}
